package sy.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.misc.BASE64Encoder; 
import sun.misc.BASE64Decoder; 
public class Base64 {
	// 将 s 进行 BASE64 编码 
	public static String getBASE64(String s) { 
		if (s == null) return null; 
		return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); 
	} 

	// 将 BASE64 编码的字符串 s 进行解码 
	public static String getFromBASE64(String s) { 
		if (s == null) return null; 
		BASE64Decoder decoder = new BASE64Decoder(); 
		try { 
			byte[] b = decoder.decodeBuffer(s); 
			return new String(b); 
		} catch (Exception e) { 
			return null; 
		} 
	}
	
	  /** 
     * 将Base64位编码的图片进行解码，并保存到指定目录 
     *  
     * @param base64 
     *            base64编码的图片信息 
     * @return 
     */  
    public static void decodeBase64ToImage(String base64, String path,  
            String imgName) {  
        BASE64Decoder decoder = new BASE64Decoder();  
        try {  
            FileOutputStream write = new FileOutputStream(new File(path  
                    + imgName));  
            byte[] decoderBytes = decoder.decodeBuffer(base64);  
            write.write(decoderBytes);  
            write.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }
    
/*    public String getFileByteString(File file) throws Exception{  
        Base64 b64 = new Base64();  
        FileInputStream fis = new FileInputStream(file);  
        System.out.print(file.length());  
        byte[] buffer = new byte[(int)file.length()];  
        System.out.print(buffer.length);  
        fis.read(buffer);  
        fis.close();  
                  
        return b64.encodeToString(buffer);  
    }*/
	public static void main(String[] args){
		String str = "";
	}
}
